package kr.hkit.score_pjt;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import kr.hkit.framework.inputPanel.ValidCheck;

public class StudentScore extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel pMain;
	private JPanel PBtn;
	private JTextField tfStdNum;
	private JTextField tfStdName;
	private JTextField tfKorScore;
	private JTextField tfEngScore;
	private JTextField tfMathScore;
	private JButton btnInput;

	////
	////https://teacher-score-pjt.googlecode.com/svn/trunk/ 쌤 svn주소
	/// http://my-coffee-pjt.googlecode.com/svn/trunk    이건 커피
	///
	
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentScore frame = new StudentScore();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StudentScore() {
		setTitle("학생 성적 처리 프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		pMain = new JPanel();
		contentPane.add(pMain, BorderLayout.CENTER);
		pMain.setLayout(new GridLayout(0, 2, 5, 5));
		
		JPanel panel = new JPanel();
		pMain.add(panel);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel = new JLabel("학번");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNewLabel);
		
		tfStdNum = new JTextField();
		panel.add(tfStdNum);
		tfStdNum.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		pMain.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel_1 = new JLabel("성명");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblNewLabel_1);
		
//		JLabel lblNewLabel_1.addFocusListener(new FocusAdapter() {
	//	});
		tfStdName.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e){
				super.focusGained(e);
				try{
					ValidCheck.isValidValue(tfStdName);
					
					
					
				}catch(Exception e1){
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
			}
		});
		
		tfStdName = new JTextField();
		panel_1.add(tfStdName);
		tfStdName.setColumns(10);
		
		JPanel panel_2 = new JPanel();
		pMain.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel_2 = new JLabel("국어점수");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		panel_2.add(lblNewLabel_2);
		
		tfKorScore = new JTextField();
		panel_2.add(tfKorScore);
		tfKorScore.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		pMain.add(panel_3);
		
		JPanel panel_4 = new JPanel();
		pMain.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel_3 = new JLabel("영어점수");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		panel_4.add(lblNewLabel_3);
		
		tfEngScore = new JTextField();
		panel_4.add(tfEngScore);
		tfEngScore.setColumns(10);
		
		JPanel panel_5 = new JPanel();
		pMain.add(panel_5);
		
		JPanel panel_6 = new JPanel();
		pMain.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel_4 = new JLabel("수학점수");
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
		panel_6.add(lblNewLabel_4);
		
		tfMathScore = new JTextField();
		panel_6.add(tfMathScore);
		tfMathScore.setColumns(10);
		
		PBtn = new JPanel();
		contentPane.add(PBtn, BorderLayout.SOUTH);
		
		btnInput = new JButton("입력");
		btnInput.addActionListener(this);
		PBtn.add(btnInput);
		
		JButton btnOutput = new JButton("출력");
		PBtn.add(btnOutput);
		
		JButton btnDeleteAll = new JButton("전체삭제");
		PBtn.add(btnDeleteAll);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnInput) {
			actionPerformedBtnInput(arg0);
		}
	}
	protected void actionPerformedBtnInput(ActionEvent arg0) {
		try {
			validCheck();
			StdInfo std = getData();
			JOptionPane.showMessageDialog(null, std + "\n 입력완료");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
		}
	}

	private void validCheck() throws Exception {
		ValidCheck.isValidValue(tfStdNum);
		ValidCheck.isValidValue(tfStdNum);
		ValidCheck.isValidValue(tfKorScore);
		ValidCheck.isValidValue(tfEngScore);
		ValidCheck.isValidValue(tfMathScore);	
	}
	
	private StdInfo getData(){
		int stdNum = Integer.parseInt(tfStdNum.getText().trim());
		String stdName = tfStdName.getText().trim();
		int korScore = Integer.parseInt(tfKorScore.getText().trim());
		int mathScore = Integer.parseInt(tfMathScore.getText().trim());
		int engScore = Integer.parseInt(tfEngScore.getText().trim());
		
		return (new StdInfo(stdNum, stdName, korScore, engScore, mathScore));
	}
}
